package org.cn.osiris.org.service.impl;

import org.cn.osiris.basic.exception.BusinessException;
import org.cn.osiris.basic.service.impl.BaseServiceImpl;
import org.cn.osiris.basic.util.BaiduAuditUtils;
import org.cn.osiris.basic.util.Md5Utils;
import org.cn.osiris.basic.util.StrUtils;
import org.cn.osiris.org.domain.Employee;
import org.cn.osiris.org.domain.Shop;
import org.cn.osiris.org.domain.ShopAuditLog;
import org.cn.osiris.org.domain.ShopDto;
import org.cn.osiris.org.mapper.EmployeeMapper;
import org.cn.osiris.org.mapper.ShopAuditLogMapper;
import org.cn.osiris.org.mapper.ShopMapper;
import org.cn.osiris.org.service.IShopService;
import org.cn.osiris.user.domain.Logininfo;
import org.cn.osiris.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void settlement(Shop shop) {
        //一：检验 - 空值校验：前端的js校验容易被禁用，不安全，后端也要做校验
        //空和空字符串返回true
        //这里就校验了一个，其实其他也需要空值校验
//        String name = shop.getName();getName
        if(StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("亲，请检查其他必填项!!!");
        }
        //校验店铺名称
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法，请重新输入！");
        }

        if(!StringUtils.isEmpty(shop.getLogo())){
            if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
                throw new BusinessException("logo不合法!!!");
            }
        }

        //二：校验 - 当前店铺是否入驻过--查询数据库是否存在数据
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){
            //查到了 = 入驻过
            throw new BusinessException("请勿重复入驻哦");
        }


        //添加logininfo信息
        Employee admin = shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.add(logininfo);


        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        //只要添加之后，就生成了自增id
        admin.setLogininfoId(logininfo.getId());
        employeeMapper.add(admin);

        //2.将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        System.out.println(admin.getId());
        shop.setAdminId(admin.getId());
        System.out.println(shop);
        shopMapper.add(shop);

        //3.将店铺的shopid设置到employee对象，修改employee信息
        //admin对象：id，shopId
        admin.setShopId(shop.getId());

        employeeMapper.update(admin);
    }

    /**
     * 审核通过
     * @param log
     * @throws MessagingException
     */
    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.修改状态：state = 2
        Shop shop = shopMapper.findById(log.getShopId());
        System.out.println(shop);
        shop.setState(2);
        shopMapper.update(shop);

        //保存审核日志
        log.setState(2);
        shopAuditLogMapper.add(log);

        //发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //如果为true 能添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("devil_osiris@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://localhost:8080/shop/active/"+log.getShopId()
                +"'>点击这里</a>激活邮件</h3>", true);

//        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://localhost:8080/shop/active'>点击这里</a>激活邮件</h3>", true);

        //店铺管理员
        Employee admin = employeeMapper.findById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);

    }

    /**
     * 审核驳货
     * @param log
     * @throws MessagingException
     */
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //1.修改状态：state = 4
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //保存审核日志
        log.setState(4);
        shopAuditLogMapper.add(log);

        //发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //如果为true 能添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("devil_osiris@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
/*        helper.setText("<h3>你提交的店铺审核未通过，请<a href='http://localhost:8080/shop/active/"+log.getShopId()
                +"'>点击这里</a>修改入驻信息并重新提交</h3>", true);*/
        helper.setText("<h3>你提交的店铺审核未通过，请<a href='http://localhost:8080/shop/active'>点击这里</a>修改入驻信息并重新提交</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.findById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    /**
     * 激活接口
     *
     *
     * 点击邮箱中的邮件链接 -----直接访问激活接口 ----
     * @param
     */
    @Override
    public void active(long id) {
        //查询到所处那过来的id对应的对象
        Shop shop = shopMapper.findById(id);

        //修改所查找到对象的状态值字段
        shop.setState(3);
        //修改对象
        shopMapper.update(shop);
        //保存审核日志
//        shopAuditLog.setState(3);
//        shopAuditLogMapper.add(shopAuditLog);

    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }


}
